package cn.hy.batch.reader;


import cn.hy.domain.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.validation.BindException;

import javax.annotation.Resource;
import java.util.List;

@Configuration
@Slf4j
public class ItemReaderFileDemo {


    //注入任务对象工厂
   @Resource
    private JobBuilderFactory jobBuilderFactory;

    //任务的执行由Step决定,注入step对象的factory
   @Resource
    private StepBuilderFactory stepBuilderFactory;


    @Resource
    ObjectMapper objectMapper;

    //创建Job对象
    @Bean
    public Job jobDemoFile() {
        return jobBuilderFactory.get("jobDemofile")
                .start(step_file())
                .build();
    }

    //创建Step对象
    @Bean
    public Step step_file() {
        return stepBuilderFactory.get("step_file")
                .<User, User>chunk(2)
                .reader(fileReader2())
                .writer(new ItemWriter<User>() {
                    @Override
                    public void write(List<? extends User> list) throws Exception {
                        System.out.println("输出list");
                        for (User user : list) {
                            System.out.println(objectMapper.writeValueAsString(user));
                        }
                    }
                })
                .build();
    }

    @Bean
    @StepScope
    public FlatFileItemReader<User> fileReader2() {
        FlatFileItemReader reader = new FlatFileItemReader();
        reader.setResource(new ClassPathResource("user.txt"));//设置要读取的文件位置
        reader.setLinesToSkip(1);//跳过第一行,这里第一行是表头

        //数据解析
        DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
        tokenizer.setNames(new String[]{"id", "name"});//按照位置与文件中的字段对应
//        tokenizer.setDelimiter(",");//设置分割字符串,默认是英文逗号，可以设置其它字符

        //把解析的数据映射为对象
        DefaultLineMapper<User> mapper = new DefaultLineMapper<>();
        mapper.setLineTokenizer(tokenizer);
        mapper.setFieldSetMapper(new FieldSetMapper<User>() {
            @Override
            public User mapFieldSet(FieldSet fieldSet) throws BindException {
                User user = new User();
                user.setId(fieldSet.readInt("id"));
                user.setName(fieldSet.readString("name"));
                return user;
            }
        });
        mapper.afterPropertiesSet();
        reader.setLineMapper(mapper);//行转换器
        return reader;
    }
}
